<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\helper;

use Yii;

/**
 * URL
 * @author cleverstone
 * @since ym2.0
 */
class Url extends \yii\helpers\Url
{
    /**
     * 绝对URL转完整URL
     * @param string $absoluteUrl 绝对URL
     * @return string
     */
    public static function whole($absoluteUrl)
    {
        if (empty($absoluteUrl)) return null;
        if (strncasecmp($absoluteUrl, 'http', 4) && strncmp($absoluteUrl, '//', 2)) {
            return rtrim(Yii::$app->request->getHostInfo(), '/') . '/' . ltrim($absoluteUrl, '/');
        }

        return $absoluteUrl;
    }

    /**
     * 附件链接转完整URL
     * @param string $link 附件链接
     * @return string
     */
    public static function attachmentUrl($link)
    {
        if (empty($link)) {
            return '';
        }

        if (
            0 === strncasecmp($link, 'http', 4)
            || 0 === strncmp($link, '//', 2)
            || Str::startsWith($link, 'data:image')
        ) {
            return $link;
        }

        $webSchema = Yii::getAlias(Yii::$app->uploadManager->provider->web);
        if (strncasecmp($webSchema, 'http', 4) && strncmp($webSchema, '//', 2)) {// 本地驱动，`webSchema`: 空字符、别名
            return Url::whole($link);
        }

        //云驱动，带Host的完整URL
        return $webSchema . $link;
    }

    /**
     * 获取后台默认头像
     * @param boolean $isAbsolutePath 是否绝对路径。
     * @return string
     */
    public static function getBackendDefaultAvatar($isAbsolutePath = false, $isRandomAvatar = false)
    {
        $filePaths = [
            '/media/image/avatar/1.jpg',
            '/media/image/avatar/2.jpg',
            '/media/image/avatar/3.jpg',
            '/media/image/avatar/4.png',
            '/media/image/avatar/5.png',
        ];
        if ($isRandomAvatar) {
            $avatarPath = $filePaths[array_rand($filePaths)];
        } else {
            $avatarPath = array_pop($filePaths);
        }

        return $isAbsolutePath ? $avatarPath : Url::whole($avatarPath);
    }

    /**
     * 文件上传，路径转数组选项
     * @param string $path
     * @return array
     */
    public static function pathToArray($path)
    {
        $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
        $options = [
            'IMAGE' => ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'],
            'VIDEO' => ['avi', 'mov', 'rmvb', 'rm', 'flv', 'mp4', '3gp'],
            'AUDIO' => ['cda', 'wav', 'mp3', 'aiff', 'aif', 'mid', 'wma', 'ra', 'vqf', 'ape', 'm4a'],
            'OTHER' => ['zip', 'rar', 'txt', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'log', 'woff', 'woff2', 'csv'],
        ];
        $type = 'OTHER';
        foreach ($options as $key => $item) {
            $item = array_map('strtolower', $item);
            if (in_array($extension, $item)) {
                $type = $key;
                break;
            }
        }

        return [
            'link' => $path,
            'url' => self::attachmentUrl($path),
            'type' => $type,
            'ext' => $extension,
        ];
    }
}